package com.ruoyi.basebill.service.impl.billIssue;

import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.mapper.TWfzRemunerationMapper;
import com.ruoyi.basebill.service.billIssue.INzjchService;
import com.ruoyi.basebill.service.impl.WfzNzjchServiceImpl;
import com.ruoyi.basebill.service.impl.billIssue.util.BillIssueUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NzjchService implements INzjchService {


    @Autowired
    private TWfzRemunerationMapper tWfzRemunerationMapper;

    @Autowired
    private BillIssueUtil billIssueUtil;

    @Autowired
    private WfzNzjchServiceImpl wfzNzjchService;

    @Override
    public int billIssue(Long billId,StringBuffer stringBuffer) {
        WfzNzjch wfzNzjch = new WfzNzjch();
        //首先根据传入的单据id，查找对应的数据
        TWfzRemuneration tWfzRemuneration = tWfzRemunerationMapper.selectTWfzRemunerationByFid(billId);
        //获取薪酬总表的单据头信息
        String fcompany = tWfzRemuneration.getFcompany();//组织
        String fperiod = tWfzRemuneration.getFperiod();//薪酬期间
        //获取当前登录的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();

        //根据组织和薪酬期间查找对应的年终奖发放数据
        WfzNzjch wfzNzjch1 = new WfzNzjch();
        wfzNzjch1.setFcompany(fcompany);
        wfzNzjch1.setFperiod(fperiod);
        List<WfzNzjch> nzjchs = wfzNzjchService.selectWfzNzjchList(wfzNzjch1);
//        //判断是否存在对应数据
//        if (nzjchs.size() == 0){
//            stringBuffer.append("<br/>"+ "单据"+ billId + "没有找到对应的【年终奖冲回】单据！");
//            return 0;
//        }
//        //判断查找的社保实际数状态是否为审核
//        wfzNzjch1 = wfzNzjchService.selectWfzNzjchByFid(nzjchs.get(0).getFid());
//        if (!wfzNzjch1.getFstate().equals("AUDIT")){
//            stringBuffer.append("<br/>"+ "单据"+ billId + "对应的【年终奖冲回】单据状态不为审核！");
//            return 0;
//        }

        ArrayList<WfzNzjchentry> wfzNzjchentries = new ArrayList<>();

        //开始遍历薪酬总表的分录信息
        List<TWfzRemunerationentry> tWfzRemunerationentryList = tWfzRemuneration.getTWfzRemunerationentryList();
        if (tWfzRemunerationentryList != null && tWfzRemunerationentryList.size() >0){
            for (int i = 0; i < tWfzRemunerationentryList.size(); i++) {
                TWfzRemunerationentry tWfzRemunerationentry = tWfzRemunerationentryList.get(i);
                String gcostnumber = tWfzRemunerationentry.getFcostnumber();
                String fcostnumber = billIssueUtil.costNumber(gcostnumber);//成本中心
                String fcostname = tWfzRemunerationentry.getFcostname();//成本中心名称
                String fcostcompany = tWfzRemunerationentry.getFcostcompany();//成本中心所属财务组织


                //获取分录的结算方式
                String fjsfs = billIssueUtil.Jsfs(fcompany, gcostnumber);
                //成本中心属性：
                String costType = billIssueUtil.costType(gcostnumber);

                String fvourtype = tWfzRemunerationentry.getFvourtype();//凭证字

                //记账公司
                //年终奖

                WfzNzjchentry wfzNzjchentry = new WfzNzjchentry();

                wfzNzjchentry.setFcostnumber(fcostnumber);
                wfzNzjchentry.setFcostname(fcostname);
                wfzNzjchentry.setFcostcompany(fcostcompany);
                wfzNzjchentry.setFjsfs(fjsfs);
                wfzNzjchentry.setFcbzxxz(costType);

                wfzNzjchentry.setFvourtype(fvourtype);

                wfzNzjchentries.add(wfzNzjchentry);//新增到单据分录
            }
        }


        //开始赋值
        billIssueUtil.setBillNumber(wfzNzjch);//单据编号
        wfzNzjch.setFcompany(billIssueUtil.costNumber(fcompany));//组织
        wfzNzjch.setFperiod(fperiod);//薪酬期间
        wfzNzjch.setFcreator(username);//制单人
        wfzNzjch.setFcreatetime(new Date());//制单时间
        wfzNzjch.setFlastupdateuser(username);//最后修改人
        wfzNzjch.setFlastupdatetime(new Date());//最后修改时间
        wfzNzjch.setFstate("SAVE");//状态
        wfzNzjch.setFsourcebillid(String.valueOf(billId));//来源单ID
        wfzNzjch.setFiseas(0L);//是否同步至eas
        String pzz = billIssueUtil.getPzz(fcompany);//凭证字
        wfzNzjch.setFvourtype(pzz);
        //类型
        wfzNzjch.setWfzNzjchentryList(wfzNzjchentries);//设置单据分录

        int i = wfzNzjchService.insertWfzNzjch(wfzNzjch);
        return i;
    }
}
